package com.yun.algorithmproblem.leetcode;

public class Leetcode2296 {

    public static void main(String[] args) {
        TextEditor obj=new TextEditor();
        obj.addText("leetcode");
        System.out.println(obj.deleteText(4));
        obj.addText("practice");
        System.out.println(obj.cursorRight(3));
        System.out.println(obj.cursorLeft(8));
        System.out.println(obj.deleteText(10));
        System.out.println(obj.cursorLeft(2));
        System.out.println(obj.cursorRight(6));
    }

    static class TextEditor {

        StringBuilder sb;
        int position;

        public TextEditor() {
            sb = new StringBuilder();
            position = 0;
        }

        public void addText(String text) {
            sb.insert(position, text);
            position += text.length();
        }

        public int deleteText(int k) {
            sb.delete(Math.max(position - k, 0), position);
            int deleteNum = Math.min(position, k);
            position=Math.max(position-k,0);
            return deleteNum;
        }

        public String cursorLeft(int k) {
            position = Math.max(position - k, 0);
            return sb.substring(position - Math.min(position,10), position);
        }

        public String cursorRight(int k) {
            position = Math.min(position + k, sb.length());
            return sb.substring(position - Math.min(position,10), position);
        }
    }
}
